<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Account extends Backend {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(['site_model','paypal/account_model','paypal/summary_model']);
		$this->data['page_home']  = lang('lang_dashboard');
		$this->data['page_title'] = "贝宝账号管理";
		$this->data['curreny'] = ["USD"=>"$","CNY"=>"¥"];
		$this->data['pstatus']=[1=>"正常",2=>"关闭",3=>"超额"];
		$this->data['pstatuslabel']=[1=>"success",2=>"danger",3=>"info"];
	}

    public function index()
    {

		if(IS_POST && $this->input->post("action"))
        {
            if($this->input->post("action")=="add")
            {
	

                $newdata=[
                    "account"=>$this->input->post("account"),
                    "limitamount"=>$this->input->post("limitamount"),
                    "limitcount"=>$this->input->post("limitcount"),		
					"note"=>$this->input->post("note"),
					"status"=>1,
					"succcount"=>0,
					"succtotal"=>0,
				];
                $this->account_model->create($newdata);
                redirect('backend/paypal/account', 'refresh');
                die();
            }
		}

		$page=max(1,(int) $this->input->get('page'));
		$key=trim($this->input->get('keyword'));
		$page_data=$this->account_model->getpage($page,$key);
	
        $this->data['data']=$page_data["data"];
        $this->data['pager']=$page_data["pager"];
		$this->data['total']=$page_data["total"];
        $this->data['pages']=$page_data["pages"];
		$this->data['page']=$page;    
	   
		$keyword = array(
			'name'      => 'keyword',
			'id'        => 'keyword',
			'value'     =>  $key,
			'class'     =>  'form-control input-sm',
			'placeholder'=> '请输入关键字',
			'maxlength' => '100'
		);

		$this->data['keyword']=$keyword;

		
		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'paypal/account';

		$this->render();
	}
	


	public function delsite()
	{
		$return=["status"=>1];
		$cid=$this->input->post("cid");
        if(!$this->account_model->dellink($cid))
        {
            $return["status"]=0;
            $return["msg"]="删除失败";
        }       
        echo json_encode($return);
        die();
	}

	public function report($acid)
	{
		$account=$this->account_model->getitembyid($acid);
		$this->data["account"]=$account;
		$this->data['page_title']=$account["account"]." 贝宝账号报表";

		$timelimit=$this->input->get("timelimit");

		if(!$timelimit)
		{
			$timelimit=[
				"start"=>date("Y-m-d",strtotime("-7 days")),
				"end"=>date("Y-m-d"),
			];
		}
		
		$mapsite=[];
		$sites=$this->site_model->getallsync();
		foreach($sites as $item)
		{
			$mapsite[$item["id"]]=$item;
		}
		$this->data["sites"]=$mapsite;


        $accountmap = $this->account_model->getaccountmap($acid);
        $result = $this->summary_model->getrangelist($timelimit,$acid);
        $this->data["result"]=$result;
        $this->data["map"]=$accountmap;

        $this->data["timerange"]=$timelimit;
        $this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'paypal/report';

		$this->render();
	}

    public function ajaxedit()
    {
        $attrName=$this->input->post("attrName");
		$newHtml=$this->input->post("newHtml");
		if(!in_array($attrName,['limitamount','limitcount','note','status'])) die();
        $cid=$this->input->post("cid");
        $newdata[$attrName]=$newHtml;
        $this->account_model->update($cid, $newdata);
        echo json_encode([]);
        die();
	}


	public function bind($acid)
	{	

		$account=$this->account_model->getitembyid($acid);
		$this->data["account"]=$account;
		$this->data['page_title']=$account["account"]." 贝宝账号绑定站点";
		
		if(IS_POST && $this->input->post("action"))
        {
            if($this->input->post("action")=="add")
            {
				
				$siteid=$this->input->post("siteid");
				$this->account_model->bind($siteid,$acid);				
                redirect('backend/paypal/account/bind/'.$acid, 'refresh');
                die();
            }
		}

		$mapsite=[];
		$sites=$this->site_model->getallsync();
		foreach($sites as $item)
		{
			$mapsite[$item["id"]]=$item;
		}
		$this->data["sites"]=$mapsite;

		$accountmap = $this->account_model->getaccountmap($acid);
		$this->data["listsites"]=$accountmap;

		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'paypal/bind';

		$this->render();
	}


	public function detail($accid)
	{

		$acc=$this->adaccount_model->getitembykey($accid);
		$this->data["account"]=$acc;

		$timelimit=$this->input->get("timelimit");

		if(!$timelimit)
		{
			$timelimit=[
				"start"=>date("Y-m-d",strtotime("-30 days")),
				"end"=>date("Y-m-d",strtotime("-1 days")),
			];
		}
		
		$result = $this->adaccountreport_model->getdetail($accid,$timelimit);
		$this->data["result"]=$result;
		$this->data["timerange"]=$timelimit;
		
		$accountmap = $this->adaccount_model->getaccountmap();
		$this->data["map"]=$accountmap;

		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'adword/detail';
		$this->render();

	}
}

